<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Identity_model extends MY_Model
{


    /**
     * 账号类型 声明
     */
    const REG_USER = 1; // 注册用户
    const MANAGE_USER = 2; // 后台管理用户
    const ADMIN_USER = 3; // 超级管理员

    /**
     * Identity_model constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 用户登陆认证 获取用户信息
     * @param string $username
     * @param string $password
     * @param int $user_type
     * @return mixed false=登陆失败; array=用户信息
     */
    public function do_auth($username, $password, $user_type)
    {
        $password = md5($password);
        switch ($user_type) {
            case self::REG_USER:
                break;
            case self::MANAGE_USER:
                $result = $this->db->select("id, user_name, real_name, group_id, type, status, create_time")
                    ->where(["user_name" => $username, "pwd" => $password])
                    ->get("system_user")->row_array();
                if (!empty($result)) {
                    return $result;
                }
                break;
            case self::ADMIN_USER:
                break;
            default:
                return false;
        }
        return false;
    }


    /**
     * 用户认证信息校验
     * @return bool false=未认证; true=已认证
     */
    public function is_auth()
    {
        if ($this->session->has_userdata('m_uid')) {
            return true;
        } else {
            redirect(base_url("/manage/login"));
            exit(0);
        }
    }


    /**
     * 用户登陆认证销毁
     */
    public function del_auth()
    {
        session_destroy();
        redirect(base_url('/manage/login'));
        exit(0);
    }


    /*********************用户角色认证*********************/
    /**
     * 前台注册用户认证
     * @return true => 认证成功, false => 认证失败
     */
    public function is_reg_user()
    {
        return $this->session->user_type == self::REG_USER;
    }

    /**
     * 后台管理员认证
     * @return true => 认证成功, false => 认证失败
     */
    public function is_manage_user()
    {
        return $this->session->user_type == self::MANAGE_USER;
    }

    /**
     * 超级管理员认证
     * @return true => 认证成功, false => 认证失败
     */
    public function is_admin_user()
    {
        return $this->session->user_type == self::ADMIN_USER;
    }

}